package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.BmsBusinessType;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppIndustry;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppIndustryChoose;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppIndustryMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.BmsBusinessTypeService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppIndustryChooseService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppIndustryService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.userChannel.ChannelBo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.industry.GetAllIndustryRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.GetCertificationBusinessRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.industry.GetAllIndustryVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * <p>
 * 电享生活行业表 服务实现类
 * </p>
 *
 * @author cfx
 * @since 2020-04-24
 */
@Service
public class DxshAppIndustryServiceImpl extends ServiceImpl<DxshAppIndustryMapper, DxshAppIndustry> implements DxshAppIndustryService {
    @Autowired
    DxshAppIndustryMapper dxshAppIndustryMapper;
    @Autowired
    BmsBusinessTypeService bmsBusinessTypeService;

    @Autowired
    DxshAppIndustryChooseService dxshAppIndustryChooseService;


    @Override
    public Map<String, Object> getAllIndustry(GetAllIndustryVo getAllIndustryVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppIndustry.IS_DELETE,Constants.COMMON_FLAG_FALSE_INT);
        List<DxshAppIndustry> dxshAppIndustries = dxshAppIndustryMapper.selectList(queryWrapper);
        List<GetAllIndustryRpVo> getAllIndustryRpVos = BeanUtils.copyList(dxshAppIndustries,GetAllIndustryRpVo.class);
        return ResultMapHelper.success(getAllIndustryRpVos, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public void postHandleUserChannel(ChannelBo channelBo) {
        List<ChannelBo> ChannelBoList = new ArrayList<>();
        ChannelBoList.add(channelBo);
        postHandleUserChannel(ChannelBoList);

    }
    @Override
    public void postHandleUserChannel(DxshAppUserChannel dxshAppUserChannel) {
        List<BmsBusinessType> bmsBusinessTypeList = bmsBusinessTypeService.getTypeByBusinessId(dxshAppUserChannel.getBusinessId());
        if(CollectionUtils.isNotEmpty(bmsBusinessTypeList) && bmsBusinessTypeList.size() > 0 ){
            BmsBusinessType bmsBusinessType =bmsBusinessTypeList.get(0);
            dxshAppUserChannel.setIndustryName(bmsBusinessType.getTypeName());
        }
    }

    @Override
    public void postHandleIsShowOther(DxshAppUserChannel dxshAppUserChannel, GetCertificationBusinessRpVo getCertificationBusinessRpVo) {
        List<BmsBusinessType> bmsBusinessTypeList = bmsBusinessTypeService.getTypeByBusinessId(dxshAppUserChannel.getBusinessId());
        if(CollectionUtils.isNotEmpty(bmsBusinessTypeList) && bmsBusinessTypeList.size() > 0 ) {
            BmsBusinessType bmsBusinessType = bmsBusinessTypeList.get(0);
            boolean flag =  IntStream.of(DxshAppIndustryService.noShowOtherIndustryArr).anyMatch(x-> x == bmsBusinessType.getId() );
            if(flag){
                getCertificationBusinessRpVo.setIsShowOther(Constants.COMMON_FLAG_TRUE_INT);
            }
        }


    }

    public void postHandleUserChannel(List<ChannelBo> channelBos) {
        if(CollectionUtils.isNotEmpty(channelBos)){
//            Set<Integer> industryId = dxshAppUserChannels.stream().map(DxshAppUserChannel::getIndustryId).collect(Collectors.toSet());
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.in(DxshAppIndustry.ID,industryId);
//            List<DxshAppIndustry> dxshAppIndustries = dxshAppIndustryMapper.selectList(queryWrapper);
//            Map<Integer,DxshAppIndustry> dxshAppIndustryMap = new LinkedHashMap<>();
//            if(CollectionUtils.isNotEmpty(dxshAppIndustries)){
//                dxshAppIndustries.forEach(di -> {
//                    dxshAppIndustryMap.put(di.getId(),di);
//                });
//            }
//            dxshAppUserChannels.forEach(du -> {
//                if(dxshAppIndustryMap.containsKey(du.getIndustryId())){
//                    du.setIndustryName(dxshAppIndustryMap.get(du.getIndustryId()).getIndustryName());
//                }
//            });
            channelBos.forEach(du ->{
                List<BmsBusinessType> bmsBusinessTypeList = bmsBusinessTypeService.getTypeByBusinessId(du.getBusinessId());
                if(CollectionUtils.isNotEmpty(bmsBusinessTypeList) && bmsBusinessTypeList.size() > 0 ){
                    BmsBusinessType bmsBusinessType =bmsBusinessTypeList.get(0);
                    du.setIndustryName(bmsBusinessType.getTypeName());
                    du.setFontColor(bmsBusinessType.getFontColor());
                    du.setBackColor(bmsBusinessType.getBackColor());
                }
            });
        }
    }


    @Override
    public void chooseIndustry(Integer userId, Integer industryId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppIndustryChoose.USER_ID,userId);
        List<DxshAppIndustryChoose> dxshAppIndustryChooses = dxshAppIndustryChooseService.list(queryWrapper);
        if(!CollectionUtils.isNotEmpty(dxshAppIndustryChooses)){
            DxshAppIndustryChoose dxshAppIndustryChoose = new DxshAppIndustryChoose();
            dxshAppIndustryChoose.setUserId(userId);
            dxshAppIndustryChoose.setIndustryId(industryId);
            dxshAppIndustryChooseService.save(dxshAppIndustryChoose);
        }
    }

    @Override
    public void chooseIndustry(Integer userId, String industryName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppIndustry.INDUSTRY_NAME,industryName);
        List<DxshAppIndustry> industries = dxshAppIndustryMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(industries)){
            DxshAppIndustry dxshAppIndustry = industries.get(0);
            chooseIndustry(userId,dxshAppIndustry.getId());
        }
    }

    @Override
    public DxshAppIndustry getIndustryByIndustryName(String industryName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppIndustry.INDUSTRY_NAME,industryName);
        List<DxshAppIndustry> industries = dxshAppIndustryMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(industries)){
            return industries.get(0);
        }
        return null;
    }

    @Override
    public Map<String, Object> getBusinessType(Integer userId) {
        DxshAppIndustry dxshAppIndustry = dxshAppIndustryChooseService.getUserChooseInIndustry(userId);
        if(dxshAppIndustry == null){

        }
        return null;
    }
}
